using System;
using System.Windows.Media.Imaging;
using AndyPike.Experiments.Fluent.Commands;
using AndyPike.Experiments.Fluent.Interfaces;

namespace AndyPike.Experiments.Fluent
{
    public class FluentImage : FluentImageBase, IFluentImage
    {
        public FluentImage(Uri uriSource)
        {
            GuardAgainst.ArgumentNull(uriSource, "uriSource");

            image = new WriteableBitmap(new BitmapImage(uriSource));
        }

        public IFluentImage ExecuteCommand(ICommand command)
        {
            GuardAgainst.ArgumentNull(command, "command");

            OnCommandStarted(command.GetType().Name);

            WriteableBitmap output = command.Execute(image);
            image = output;
            
            OnCommandCompleted(command.GetType().Name);

            return this;
        }

        public IFluentImage Brightness(int level)
        {
            return ExecuteCommand(new BrightnessCommand(level));
        }

        public IFluentImage Contrast(int level)
        {
            return ExecuteCommand(new ContrastCommand(level));
        }

        public IFluentImage Greyscale()
        {
            return ExecuteCommand(new GreyscaleCommand());
        }

        public IFluentImage SaveAsJpeg(string path, int qualityLevel)
        {
            return ExecuteCommand(new SaveAsJpegCommand(path, qualityLevel));
        }

        public IFluentImage Resize(int width, int height, ResizeMode mode)
        {
            return ExecuteCommand(new ResizeCommand(width, height, mode));
        }
    }
}